package com.gec.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gec.entity.Shop;
import com.gec.service.ShopService;
import com.gec.service.impl.ShopServiceImpl;
@WebServlet("/melist")
public class ListServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String searchs = req.getParameter("search");
		int page = 1;
		if(req.getParameter("page")!=null)
			page = Integer.parseInt(req.getParameter("page"));
		ShopService service = new ShopServiceImpl();
		List<Shop> list = null;
		long rowCount = 0;
		if(searchs==null) {
			 list = service.findPage(page,3);		
			 rowCount = service.findRowCount();//不带条件的总行数
		}else {
			list = service.findPageByName("%"+searchs+"%",page,3);
			rowCount = service.findRowCount("%"+searchs+"%");//带条件的总行数
		}
		//算出总页数
		int pageCount = (int)(rowCount/3);  //总页面
		if (rowCount%3>0)//只要有数据多,就要加一行
			pageCount++;
		req.setAttribute("rowCount", rowCount);
		req.setAttribute("pageCount", pageCount);
		req.setAttribute("list", list);
		req.getRequestDispatcher("/WEB-INF/jsp/melist.jsp").forward(req, resp);
	}

	
}